package com.platform.controller;

import com.platform.entity.TeamEntity;
import com.platform.service.InviteService;
import com.platform.service.TeamService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推广管理:团队管理
 *
 */
@RestController
@RequestMapping("team")
public class TeamController {
    @Autowired
    private TeamService teamService;

    @Autowired
    private InviteService inviteService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("team:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TeamEntity> teamList = teamService.queryList(query);
        //获取对应UID当日团队增加人数
        for(TeamEntity teamEntity : teamList){
            Map<String, Object> map = new HashMap<>();
            map.put("uId", teamEntity.getUserId());
            int addNum = inviteService.queryTotal(map);
            teamEntity.setAddNum(addNum);
        }
        int total = teamService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(teamList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("team:info")
    public R info(@PathVariable("id") Long id) {
        TeamEntity team = teamService.queryObject(id);

        return R.ok().put("team", team);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("team:save")
    public R save(@RequestBody TeamEntity team) {
        teamService.save(team);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("team:update")
    public R update(@RequestBody TeamEntity team) {
        teamService.update(team);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("team:delete")
    public R delete(@RequestBody Long[] ids) {
        teamService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<TeamEntity> list = teamService.queryList(params);

        return R.ok().put("list", list);
    }
}
